package com.huawei.spare.part.replace.rx;

import com.huawei.spare.part.replace.framework.SmartContentPresenter;
import io.reactivex.ObservableTransformer;
import io.reactivex.rxjavafx.schedulers.JavaFxScheduler;
import io.reactivex.schedulers.Schedulers;

public class RxUtils {

  /**
   * Run on io thread and subscribe on UI thread
   * .compose(RxUtils.<T>applySchedulers())
   */
  public static <T> ObservableTransformer<T, T> applySchedulers() {
    return observable -> observable.subscribeOn(Schedulers.io())
        .observeOn(JavaFxScheduler.platform());
  }

  /**
   * Run on io thread and subscribe on UI thread
   * .compose(RxUtils.<T>applySchedulers())
   */
  public static <T> ObservableTransformer<T, T> loadingTask(final SmartContentPresenter presenter) {
    return observable -> observable.subscribeOn(Schedulers.io())
        .observeOn(JavaFxScheduler.platform())
        .doFinally(() -> presenter.dismissLoading());
  }
}
